﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;

namespace IEx.Utilities
{
    public class MiscUtility
    {
        public static string UpdateQueryStringItem(HttpRequest httpRequest, string queryStringKey, string newQueryStringValue)
        {
            StringBuilder NewURL = new StringBuilder();

            NewURL.Append(httpRequest.RawUrl);

            if (httpRequest.QueryString[queryStringKey] != null)
            {
                string OrignalSet = String.Format("{0}={1}", queryStringKey, httpRequest.QueryString[queryStringKey]);
                string NewSet = String.Format("{0}={1}", queryStringKey, newQueryStringValue);
                NewURL.Replace(OrignalSet, NewSet);
            }
            else if (httpRequest.QueryString.Count == 0)
            {
                NewURL.AppendFormat("?{0}={1}", queryStringKey, newQueryStringValue);
            }
            else
            {
                NewURL.AppendFormat("&{0}={1}", queryStringKey, newQueryStringValue);
            }

            return NewURL.ToString();
        }

        public static string UpdateQueryStringItem(HttpRequest httpRequest, string[] queryStringKeys, string[] newQueryStringValues)
        {
            StringBuilder NewURL = new StringBuilder();

            NewURL.Append(httpRequest.RawUrl.Replace("%20", " "));
            bool check = true;
            for (int i = 0; i < queryStringKeys.GetLength(0); i++)
            {
                string queryStringKey = queryStringKeys[i];
                string newQueryStringValue = newQueryStringValues[i];
                if (httpRequest.QueryString[queryStringKey] != null)
                {
                    string OrignalSet = String.Format("{0}={1}", queryStringKey, httpRequest.QueryString[queryStringKey]);
                    string NewSet = String.Format("{0}={1}", queryStringKey, newQueryStringValue);
                    NewURL.Replace(OrignalSet, NewSet);
                }
                else if (httpRequest.QueryString.Count == 0)
                {
                    if (newQueryStringValue != "" && newQueryStringValue != null)
                    {
                        if (check)
                        {
                            NewURL.AppendFormat("?{0}={1}", queryStringKey, newQueryStringValue);
                            check = false;
                        }
                        else NewURL.AppendFormat("&{0}={1}", queryStringKey, newQueryStringValue);
                    }
                }
                else if (newQueryStringValue != "" && newQueryStringValue != null) NewURL.AppendFormat("&{0}={1}", queryStringKey, newQueryStringValue);
            }

            return NewURL.ToString();
        }        

        public static string StringIndent(int level)
        {
            string retVal = string.Empty;
            for (int i = 0; i < level; i++)
                retVal += ".....";
            return retVal;
        }

        public static string GetSubstring(string Content, int leng)
        {
            if (Content.Length > leng)
            {
                string temp = Content.Substring(0, leng);
                temp = temp.Replace("\r", "").Replace("\n", "").Replace("\t", "");
                Content = temp.Substring(0, temp.LastIndexOf(" ")) + "...";
            }
            else
            {
                Content = Content.Replace("\r", "").Replace("\n", "").Replace("\t", "");
            }
            return Content;
        }

        public static string RemoveSpecialCharacter(string s)
        {
            string rgPattern = @"[^A-Za-z0-9 ]";
            Regex oRegex = new Regex(rgPattern);
            s = oRegex.Replace(s, string.Empty);
            return s;
        }

        public static string StandardString(string dataValue)
        {
            dataValue = dataValue.Replace("\"", "'");
            dataValue = dataValue.Replace("\\", "&#92");
            dataValue = dataValue.Replace("'", "&#146");
            dataValue = dataValue.Replace(Convert.ToChar(10).ToString(), "<br>");
            dataValue = dataValue.Replace(Convert.ToChar(13).ToString(), "<br>");
            return dataValue;
        }

        public static string Truncate(string input, int characterLimit)
        {
            //Can phai chuan hoa chuoi nhap vao 
            string output = input;

            // Check if the string is longer than the allowed amount
            // otherwise do nothing
            if (output.Length > characterLimit && characterLimit > 0)
            {

                // cut the string down to the maximum number of characters
                output = output.Substring(0, characterLimit);

                // Check if the space right after the truncate point 
                // was a space. if not, we are in the middle of a word and 
                // need to cut out the rest of it
                if (input.Substring(output.Length, 1) != " ")
                {
                    int LastSpace = output.LastIndexOf(" ");

                    // if we found a space then, cut back to that space
                    if (LastSpace != -1)
                    {
                        output = output.Substring(0, LastSpace);
                    }
                }
                // Finally, add the "..."
                output += "...";
            }
            return output;
        }

        public static string ProcessString(string s)
        {
            s = RemoveSpecialCharacter(s);
            s = s.Replace(" ", "-");
            s = s.Replace("--", "-");
            s = s.ToLower();
            return s;
        }
    }

}
